var voucherList = {};
var VoucherList = function () {
    //初始化函数
    this.init = function () {
        var paramMap = layuiFunciton.getURLParamMap();
        if (paramMap) {
            $("#tranType").val(paramMap["tranType"])
        }
        //初始化时间选择
        layuiFunciton.layTime({
            elem: '#startDate'
            , format: 'yyyy-MM-dd'
        });
        layuiFunciton.layTime({
            elem: '#endDate'
            , format: 'yyyy-MM-dd'
        });
        //初始化表格
        layuiFunciton.tableInit({
            toolbar: '.btn',
            tableId: 'voucherListTableId',
            url: layuiFunciton.URL + '/wb/voucher/selectPage',
            id: 'voucherListId',
            searchFromId: 'formVoucher',
            totalRow: true,
            tableFilter: 'voucherListFilter',  //表格监听名称,
            cols: [[
                {type: 'numbers', title: '序号', fixed: 'left'},
                {field: 'settlementNumber', title: '结算编号', sort: true, fixed: 'left', totalRowText: '合计：'}
                , {field: 'dateReceipt', width: 120, title: '付款日期', sort: true}
                , {
                    field: 'paymentAccount', title: '收款账户', sort: true,
                    templet: function (row) {
                        return layuiFunciton.getValNameByValCode(row.paymentAccount);
                    }
                }
                , {
                    field: 'drawee', width: 100, title: '付款人', sort: true
                }
                , {
                    field: 'paymentAmount',
                    width: 100,
                    align: "center",
                    title: '付款金额',
                    totalRowText: '<span style="color: #009feb" class="paymentAmount">0.00</span>',
                    sort: true,
                    templet: function (row) {
                        return '<span style="color: #009feb">' + Number(row.paymentAmount).toFixed(2) + '</span>';
                    }
                }
                , {
                    field: 'amountCancelVerification',
                    width: 120,
                    align: "center",
                    title: '已核销金额',
                    totalRowText: '<span style="color: red" class="amountCancelVerification">0.00</span>',
                    sort: true,
                    templet: function (row) {
                        if (row.amountCancelVerification) {
                            return '<span style="color: red">' + Number(row.amountCancelVerification).toFixed(2) + '</span>';
                        } else {
                            return '<span style="color: red">0.00</span>';
                        }
                    }
                }
                , {
                    field: '',
                    title: '余额',
                    width: 100,
                    totalRowText: '<span style="color: green" class="yue">0.00</span>',
                    align: "center",
                    sort: true,
                    templet: function (row) {
                        return '<span style="color: green">' + (Number(row.paymentAmount) - Number(row.amountCancelVerification)).toFixed(2) + '</span>';
                    }
                }
                , {field: 'note', title: '备注', sort: true}
                , {
                    title: '操作',
                    width: 130,
                    align: "center",
                    templet: '#voucherBar',
                    fixed: 'right',
                    templet: function (row) {
                        return '<a href="javascript:void(0)" style="color:#009feb" lay-event="details">明细</a> <a href="javascript:void(0)" style="color:#009feb" lay-event="exportInfo">导出明细</a>';
                    }
                }
            ]],
            returnData: function (res) {
                var paymentAmount = 0.00, amountCancelVerification = 0.00;
                res.forEach(function (obj, index) {
                    paymentAmount = (Number(paymentAmount) + Number(obj.paymentAmount)).toFixed(2);
                    amountCancelVerification = (Number(amountCancelVerification) + Number(obj.amountCancelVerification)).toFixed(2);
                });
                $(".paymentAmount").html(paymentAmount);
                $(".amountCancelVerification").html(amountCancelVerification);
                $(".yue").html((Number(paymentAmount) + Number(amountCancelVerification)).toFixed(2));
            },
            tools: {
                details: function (row) {
                    layuiFunciton.layerOpen({
                        title: "收款核销明细",
                        finder: "form",
                        area: ['80%', '80%'], //自定义宽高
                        areaType: true,
                        content: "details.html",
                        data: row,
                        end: function () {
                            layuiFunciton.refresh('formVoucher', 'voucherListId');
                        }
                    });
                },
                export: function () {
                    location.href = layuiFunciton.URL + '/wb/voucher/exportRecords?' + $("#formVoucher").serialize();
                },
                exportInfo:function (row) {
                    location.href = layuiFunciton.URL + '/wb/voucher/exportInfo?settlementNumber=' + row.settlementNumber;
                }
            }
        });
    };
    //初始化下拉列表
    this.initDict = function () {
        //初始化搜索条件收款账户
        layuiFunciton.selectDict("paymentAccount", "SKZH");
    };
    //注册事件
    this.event = function () {
        $("#search").click(function () {
            if($("#startDate").val().length == 0 || $("#endDate").val().length == 0){
                if(!($("#startDate").val().length == 0 && $("#endDate").val().length == 0)){
                    layuiFunciton.warn('开始时间和结束时间必填！');
                    return;
                }
            }
            if($('#startDate').val() && $('#endDate').val()){
                $("#dateReceipt").val($('#startDate').val() + ' ~ ' + $('#endDate').val());
            }else{
                $('#startDate').val("");
                $('#endDate').val("");
                $("#dateReceipt").val("");
            }
            layuiFunciton.refresh('formVoucher', 'voucherListId', 1);
        });
    }
};
voucherList = new VoucherList();
voucherList.initDict();
voucherList.init();
voucherList.event();
